/// -----------------------------------------------------------------------------------------------------------
/// Module      :  SalesForceWCFAdapterBindingElementExtensionElement.cs
/// Description :  This class is provided to surface Adapter as a binding element, so that it 
///                can be used within a user-defined WCF "Custom Binding".
///                In configuration file, it is defined under
///                <system.serviceModel>
///                  <extensions>
///                     <bindingElementExtensions>
///                         <add name="{name}" type="{this}, {assembly}"/>
///                     </bindingElementExtensions>
///                  </extensions>
///                </system.serviceModel>
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceModel.Channels;
using System.Configuration;
using System.Globalization;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace WCFLOBTalk.SalesForceWCF
{
    using System;
    using System.Configuration;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Configuration;

    public class SalesForceWCFAdapterBindingElementExtensionElement : BindingElementExtensionElement
    {

        #region  Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public SalesForceWCFAdapterBindingElementExtensionElement()
        {
        }

        #endregion Constructor

        #region Custom Generated Properties


        [ConfigurationProperty("host", DefaultValue = "test.salesforce.com")]
        public string Host
        {
            get
            {
                return (string)base["host"];
            }
            set
            {
                base["host"] = value;
            }
        }

        [ConfigurationProperty("hostType", DefaultValue = HostTypeEnum.u)]
        public HostTypeEnum HostType
        {
            get
            {
                return (HostTypeEnum)base["hostType"];
            }
            set
            {
                base["hostType"] = value;
            }
        }

        [ConfigurationProperty("version", DefaultValue = "8.0")]
        public string Version
        {
            get
            {
                return (string)base["version"];
            }
            set
            {
                base["version"] = value;
            }
        }

        [ConfigurationProperty("protocol", DefaultValue = ProtocolTypesEnum.SOAP)]
        public ProtocolTypesEnum Protocol
        {
            get
            {
                return (ProtocolTypesEnum)base["protocol"];
            }
            set
            {
                base["protocol"] = value;
            }
        }

        [ConfigurationProperty("securityToken", DefaultValue = "")]
        public string SecurityToken
        {
            get
            {
                return (string)base["securityToken"];
            }
            set
            {
                base["securityToken"] = value;
            }
        }

        [ConfigurationProperty("username", DefaultValue="")]
        public string Username
        {
            get
            {
                return (string)base["username"];
            }
            set
            {
                base["username"] = value;
            }
        }

        [ConfigurationProperty("password", DefaultValue="")]
        public string Password
        {
            get
            {
                return (string)base["password"];
            }
            set
            {
                base["password"] = value;
            }
        }
        

        #endregion Custom Generated Properties

        #region BindingElementExtensionElement Methods
        /// <summary>
        /// Return the type of the adapter (binding element)
        /// </summary>
        public override Type BindingElementType
        {
            get
            {
                return typeof(SalesForceWCFAdapter);
            }
        }
        /// <summary>
        /// Returns a collection of the configuration properties
        /// </summary>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                ConfigurationPropertyCollection properties = base.Properties;

                properties.Add(new ConfigurationProperty("host", typeof(string), "test.salesforce.com"));
                properties.Add(new ConfigurationProperty("hostType", typeof(HostTypeEnum), HostTypeEnum.u));
                properties.Add(new ConfigurationProperty("protocol", typeof(ProtocolTypesEnum), ProtocolTypesEnum.SOAP));
                properties.Add(new ConfigurationProperty("securityToken", typeof(string), ""));
                properties.Add(new ConfigurationProperty("version", typeof(string), "8.0"));
                properties.Add(new ConfigurationProperty("username", typeof(string), ""));
                properties.Add(new ConfigurationProperty("password", typeof(string), ""));

                return properties;
            }
        }

        /// <summary>
        /// Instantiate the adapter.
        /// </summary>
        /// <returns></returns>
        protected override BindingElement CreateBindingElement()
        {
            SalesForceWCFAdapter adapter = new SalesForceWCFAdapter();
            this.ApplyConfiguration(adapter);
            return adapter;
        }

        /// <summary>
        /// Apply the configuration properties to the adapter.
        /// </summary>
        /// <param name="bindingElement"></param>
        public override void ApplyConfiguration(BindingElement bindingElement)
        {
            base.ApplyConfiguration(bindingElement);
            SalesForceWCFAdapter adapterBinding = ((SalesForceWCFAdapter)(bindingElement));

            adapterBinding.Host = this.Host;
            adapterBinding.HostType = this.HostType;
            adapterBinding.Protocol = this.Protocol;
            adapterBinding.SecurityToken = this.SecurityToken;
            adapterBinding.Version = this.Version;

        }

        /// <summary>
        /// Initialize the binding properties from the adapter.
        /// </summary>
        /// <param name="bindingElement"></param>
        protected override void InitializeFrom(BindingElement bindingElement)
        {
            base.InitializeFrom(bindingElement);
            SalesForceWCFAdapter adapterBinding = ((SalesForceWCFAdapter)(bindingElement));

            this.Host = adapterBinding.Host;
            this.HostType = adapterBinding.HostType;
            this.Protocol = adapterBinding.Protocol;
            this.SecurityToken = adapterBinding.SecurityToken;
            this.Version = adapterBinding.Version;

        }

        /// <summary>
        /// Copy the properties to the custom binding
        /// </summary>
        /// <param name="from"></param>
        public override void CopyFrom(ServiceModelExtensionElement from)
        {
            base.CopyFrom(from);
            SalesForceWCFAdapterBindingElementExtensionElement adapterBinding = ((SalesForceWCFAdapterBindingElementExtensionElement)(from));

            this.Host = adapterBinding.Host;
            this.HostType = adapterBinding.HostType;
            this.Protocol = adapterBinding.Protocol;
            this.SecurityToken = adapterBinding.SecurityToken;
            this.Version = adapterBinding.Version;

        }

        #endregion BindingElementExtensionElement Methods
    }
}

